#include "Widget.h"
#include "ui_Widget.h"
#include <QPainter>
#include <QLibrary>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    QRect rect(0, 0, width(), height());

    painter.setViewport(rect);  //设置 Viewport
    painter.setWindow(0, 0, 100, 50); //设置窗口大小，逻辑坐标

    painter.setPen(m_pen);
    painter.drawRect(10, 10, 80, 30) ;
}


void Widget::on_BtnSetPen_clicked()
{
    //设置pen
    //    bool ok = false;
    //    QPen pen = DialogPen::getPen(m_pen, ok);

    //    if(ok)
    //    {
    //        m_pen = pen;
    //        this->repaint();
    //    }

}


void Widget::on_BtnByDynamicDllLoader_clicked()
{
    QLibrary myLib ("DynamicLibraryDemo.dll") ;
    if(myLib.load()){
        if (myLib.isLoaded() )
            QMessageBox::information(this, "信息", "DynamicLibraryDemo.DLL loader");

        typedef  QPen (*FunGetPen)(QPen iniPen, bool &ok) ;
        FunGetPen myGetPen = (FunGetPen)myLib.resolve ("getPen"); //解析 DLL中的函数
        //设置pen
        bool ok = false;
        if(myGetPen)
        {

            QPen pen = myGetPen(m_pen,ok);
            if(ok)
            {
                m_pen = pen;
                this->repaint();
            }
        }
        else
        {

            QMessageBox::warning(this, "警告",myLib.errorString());
            return;
        }
    }
    else
    {
        QMessageBox::warning(this, "警告",myLib.errorString());

    }
}

